var IS_IE = true;
try
{
	window.attachEvent = window.HTMLDocument.prototype.attachEvent = window.HTMLElement.prototype.attachEvent = function (type, callback) {this.addEventListener(type.substring(2), callback, false);}
	window.detachEvent = window.HTMLDocument.prototype.detachEvent = window.HTMLElement.prototype.detachEvent = function (type, callback) {this.removeEventListener(type.substring(2),callback,false);}

	CSSStyleDeclaration.prototype.__defineGetter__('pixelLeft',function() {return parseInt(this.left) || 0;});
	CSSStyleDeclaration.prototype.__defineSetter__('pixelLeft',function(i) {this.left = i + "px";});
	CSSStyleDeclaration.prototype.__defineGetter__('pixelTop',function() {return parseInt(this.top) || 0;});
	CSSStyleDeclaration.prototype.__defineSetter__('pixelTop',function(i) {this.top = i + "px";});

	IS_IE = false;
}
catch(e) { }

function Queue()
{
	var queue = new Array();
	var space = 0;

	this.enqueue = function(element)
	{
		queue.push(element);
	}

	this.dequeue = function()
	{
		if (queue.length)
		{
			var element=queue[space];

			if (++space * 2 >= queue.length)
			{
				for (var i = space; i < queue.length; i++) 
					queue[i - space] = queue[i];

				queue.length -= space;
				space=0;
			}

			return element;
		}
		else
			return undefined;
	 }
}
	
var EasyAjax = {	
	
	callQueue : new Queue(),
	
	waiting : false,

	callNext : function ()
	{
		if (!EasyAjax.waiting)
		{
			var queuedCall = EasyAjax.callQueue.dequeue();
			
			if (queuedCall != undefined)
			{		
				EasyAjax.waiting = true;

				var url = queuedCall[0];
				var params = queuedCall[1];
				var callback = queuedCall[2];
				
				var content = "";

				if (params)
					for (var key in params)
						content += (key + "=" + encodeURI(params[key]) + "&");

				var xmlhttp;

				if (window.XMLHttpRequest)
					xmlhttp = new XMLHttpRequest();
				else if (window.ActiveXObject)
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

				setTimeout
				(
					function () 
					{ 
						if (xmlhttp.readyState != 4)
						{
							xmlhttp.abort();
							EasyAjax.waiting = false; 
							EasyAjax.callNext();
						}
					},
					10000
				);

				if (callback)
					xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { callback(xmlhttp.responseText); EasyAjax.waiting = false; EasyAjax.callNext(); } };

				if (params)
				{
					xmlhttp.open("POST",url,true);

					xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlhttp.setRequestHeader("Content-length", content.length);
					xmlhttp.setRequestHeader("Connection", "close");

					xmlhttp.send(content);
				}
				else
				{
					if (url.indexOf("?") == -1)
						url += "?";
					else 
						url += "&";
					url += "n=" + Math.floor(Math.random()*999999999)

					xmlhttp.open("GET",url,true);
					xmlhttp.send(null);		
				}
			}
		}
	},

	reload : function(node, params)
	{
		EasyAjax.callQueue.enqueue([node.getAttribute("url"), params, function (responseText) { node.innerHTML = responseText; }]);
		EasyAjax.callNext();	
	},

	init : function()
	{
		EasyAjax.step(document.body);
	},

	step : function(node)
	{
		if (node.nodeName.toUpperCase() == "EASY:AJAX" || node.nodeName.toUpperCase() == "AJAX")
		{
			node.reload = function (params) { EasyAjax.reload(node, params); }

			if (node.getAttribute("interval")) 
			{
				node.reload();
				setInterval(function () { node.reload(); }, node.getAttribute("interval"));	
			}
		}

		for (var i = 0; i < node.childNodes.length; i++)
			EasyAjax.step(node.childNodes[i]);
	}
	
};

window.attachEvent("onload", EasyAjax.init);